<template>
  <div class="header">
    <div class="header-title">
      <span @click = "toggleAll" v-if="checkedNote.length == dataList.length && dataList.length !== 0">取消全选 </span>
      <span @click = "checkAll" v-else>全选 </span>
      <span>已选择{{checkedNote.length}}项</span>
      <svgIcon name="清除" style="color: #000;" size="24" @click="emit('back')"></svgIcon>
    </div>
    <div class="header-icon" style="width: 100%;">
      <svgIcon name="笔记3" style="color: #000;" size="24"></svgIcon>
      <svgIcon name="AI筛选" @click="show = true" size="70"></svgIcon>
    </div>
  </div>
  <div class="noteCheckBox">
    <van-checkbox-group v-model="checkedNote" ref="checkBoxGroup">
      <div class="card" v-for="item in dataList" :key="item.id">
        <van-checkbox :name="item" icon-size="14px">
          <div class="card-content-time"><span style="margin-right: 8px;">{{ moment(item.create_time).format('MM/DD HH:mm') }}</span><span>{{ item.address }}</span></div>
        </van-checkbox>
        <div class="card-content">
          <div class="card-content-title">笔记标题—{{item.file_name}}</div>
        </div>
      </div>
    </van-checkbox-group>
    <div class="footer">
      <van-button type="primary" style="width: 100%;" round @click="selected">添加</van-button>
    </div>

    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="block">
          <div class="title"><span>AI</span><span>筛选</span></div>
          <div class="time">
            <span>时间</span>
            <van-cell-group style="display:flex;">
              <van-field readonly clickable name="calendar" v-model="formData.startDate" :value="formData.startDate"
                placeholder="开始时间" @click="showCalendarStart = true" />
              <van-field readonly clickable clearable name="calendar" v-model="formData.endDate"
                :value="formData.endDate" placeholder="结束时间" @click="showCalendarEnd = true">
                <template #right-icon>
                  <div class="search-icon">
                    <SvgIcon v-show="formData.startDate.length !== 0 || formData.endDate.length !== 0"
                      @click.stop="clearDate" class="icon-item" style="color: #fff;" name="清空" :size="24" />
                  </div>

                </template>
              </van-field>
            </van-cell-group>
            <van-calendar v-model:show="showCalendarStart" @confirm="onConfirmStart" :min-date="startDate"/>
            <van-calendar v-model:show="showCalendarEnd" @confirm="onConfirmEnd" :min-date="endDate"/>
          </div>
          <div class="addr">
            <span>地点</span>
            <van-cell-group class="input">
              <van-field style="background-color: #f0f0f0;" v-model="formData.addr" placeholder="例如：北京市">

                <template #right-icon>
                  <div class="search-icon">
                    <SvgIcon v-show="formData.addr.length !== 0" @click="formData.addr = ''" style="color: #fff;"
                      class="icon-item" name="清空" :size="24" />
                  </div>
                </template>
              </van-field>
            </van-cell-group>
          </div>
          <div class="searchAI">
            <span>AI搜索关键信息</span>
            <van-cell-group class="input">
              <van-field style="background-color: #f0f0f0;" v-model="formData.search" placeholder="上周的工作笔记">
                <template #right-icon>
                  <div class="search-icon">
                    <SvgIcon v-show="formData.search.length !== 0" @click="formData.search = ''" class="icon-item"
                      name="清空" :size="24" />
                  </div>
                </template>

              </van-field>
            </van-cell-group>
          </div>
          <div class="block-footer">
            <van-button plain type="primary" @click="show = false">取消</van-button>
            <van-button plain type="primary" @click="filter">确定</van-button>
          </div>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue"
import {filtration, listNote} from "@/api/index.js";
import SvgIcon from "@/components/SvgIcon.vue";
import moment from "moment";

const dataList = ref([])
const show = ref(false)
const showCalendarStart = ref(false)
const showCalendarEnd = ref(false)
const emit = defineEmits(['back','selectedData'])
const checkedNote = ref([])
const checkBoxGroup =ref(null);
const startDate = ref(new Date(2000, 1, 1))
const endDate = ref(new Date(2000, 1, 1))
const onConfirmStart = (date) => {
  endDate.value = new Date(date.getFullYear(),date.getMonth(),date.getDate());
  formData.value.startDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 00:00:00`;
  showCalendarStart.value = false;
}
const onConfirmEnd = (date) => {
  formData.value.endDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} 23:59:59`;
  showCalendarEnd.value = false;
}
const formData = ref(
  {
    title: '',
    startDate: '',
    endDate: '',
    addr: '',
    search: '',
    filePaths: []
  }
)
onMounted(async () => {
  const data = await listNote()
  console.log(data)
  dataList.value = data.data
})

const filter = async() =>{
  const data = await filtration({start_time_str:formData.value.startDate,end_time_str:formData.value.endDate,address:formData.value.addr,content:formData.value.search})
  dataList.value = data.data
  show.value = false
}
const selected = () =>{
  console.log(checkedNote.value)
  emit('selectedData', checkedNote.value);
}
const checkAll = () => {
  checkBoxGroup.value.toggleAll(true);
}
const toggleAll = () =>{
  checkBoxGroup.value.toggleAll();
}
//清空时间
const clearDate = () => {
  formData.value.startDate = ''
  formData.value.endDate = ''

}
</script>

<style lang="scss" scoped>
.header {
  height: 110px;
  border-radius: 7px;
  padding: 10px 20px;
  display: flex;
  justify-content: space-between;
  margin-top: 10px;
  align-items: center;
  flex-direction: column;

  span {
    color: #191919;
    font-size: 16px;
    font-weight: 600;
    display: flex;
    justify-content: start;
  }

  .header-title {
    display: flex;
    justify-content: space-between;
    width: 100%;
    align-items: center;

    span {
      &:nth-child(1) {
        text-decoration: none;
        color: var(--van-primary-color);
        font-family: "PingFang SC";
        font-size: 15px;

      }

      &:nth-child(2) {
        font-size: 17px;
      }
    }
  }

  .header-icon {
    display: flex;
    margin-top: 10px;
    justify-content: space-between;
  }
}

.noteCheckBox {
  width: 100%;
  padding: 0 20px;
  height: 470px;
  overflow: hidden;
  overflow-y: auto;

  .card {
    width: 100%;
    height: 80px;
    border-radius: 12px;
    background: #FFF;
    margin: 8px 0;
    padding: 10px;

    .card-content-time {
      span {
        color: #666;
        font-size: 13px;
      }
    }

    .card-content-title {
      margin-top: 10px;
      color: #191919;
      font-family: "PingFang SC";
      font-size: 15px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      -o-text-overflow: ellipsis;
    }
  }

  .footer {
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    height: 80px;
    background: #FFF;
    padding: 0 60px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .wrapper {
    display: flex;
    justify-content: center;
    height: 100%;

    .block {
      margin-top: 60px;
      width: 351px;
      height: 425px;
      background-color: #fff;
      border-radius: 16px;
      position: relative;

      .title {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 25px;

        span {
          font-size: 17px;

          &:nth-child(1) {
            color: var(--van-primary-color);
            font-weight: 600;
          }
        }
      }

      .time {
        padding: 0 20px;

        span {
          font-size: 14px;
          font-weight: 400;
        }

        :deep(.van-cell--clickable) {
          background-color: rgb(240, 240, 240);
        }
      }

      .addr {
        padding: 0 20px;

        margin-top: 10px;

        span {
          font-size: 14px;
          font-weight: 400;
        }

        .input {
          margin-top: 15px;
        }
      }

      .searchAI {
        padding: 0 20px;
        margin-top: 10px;

        span {
          font-size: 14px;
          font-weight: 400;
        }

        .input {
          margin-top: 15px;

          .search-icon {
            display: flex;
            align-items: center;
            justify-content: center;

            .icon-item {
              display: flex;
              align-items: center;
              justify-content: center;
              margin-right: 15px;

              color: #fff;

              &:nth-child(2) {
                border-radius: 50%;
                padding: 4px;
                color: #000000;
                transition: 1s;
              }
            }

            .active {
              background-color: var(--van-primary-color);
              color: #fff !important;
            }
          }
        }
      }

      .block-footer {
        display: flex;
        width: 100%;
        position: absolute;
        bottom: 0;
        left: 0;

        button {
          width: 50%;
          border: none;
        }
      }
    }
  }

}
</style>
